/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import React, { useMemo } from 'react';
import type { ProColumns } from '@ant-design/pro-table';
import type { FixedType } from 'rc-table/lib/interface';
import PreviewImage from './PreviewImage';
import { Space, Popconfirm } from 'antd';
import FailReason from './FailReason';
import { deleteInvoiceOrder, getGDinvoice, openInvoiceServe } from '@/services/finance_receipt';
import InvoiceTitle from './InvoiceTitle';
import RejectReason from './RejectReason';

// 查看电子面单
export const electronicInvoice = (props: {
  fixed?: FixedType;
  title?: string;
  dataIndex?: string;
  width?: string | number;
}): ProColumns => {
  const { fixed = false, title = '电子发票', dataIndex = 'gd_pdf_url', width = '5%' } = props;
  return {
    title,
    dataIndex,
    width,
    fixed,
    align: 'center',
    search: false,
    render: (_, record) => {
      return record[dataIndex] ? (
        <PreviewImage src={record[dataIndex] || ''}>
          <a>查看</a>
        </PreviewImage>
      ) : (
        ''
      );
    },
  };
};

export const useOperationWkd = (props: {
  fixed?: FixedType;
  width?: string | number;
  refreshList: () => void;
  wkdOperatonStatus: string;
}): ProColumns => {
  const { fixed = false, width, refreshList, wkdOperatonStatus } = props;

  const isShowInvoice = useMemo(() => ['1', '3'].includes(wkdOperatonStatus), [wkdOperatonStatus]);

  return isShowInvoice
    ? {
        title: '操作',
        dataIndex: 'operation',
        align: 'center',
        fixed,
        search: false,
        width,
        render: (_, record) => {
          const { status, fail_reasons = [], id, invoice_id } = record;
          return (
            <Space>
              <Popconfirm
                title='确定开票？'
                onConfirm={openInvoiceServe.bind(null, { id, callback: refreshList })}
              >
                <a>开票</a>
              </Popconfirm>
              {status != '3' && (
                <Popconfirm
                  title='确定作废？'
                  onConfirm={deleteInvoiceOrder.bind(null, { invoice_id, callback: refreshList })}
                >
                  <a>作废</a>
                </Popconfirm>
              )}
              {!!(fail_reasons.length && status == 3) && <FailReason fail_reasons={fail_reasons} />}
            </Space>
          );
        },
      }
    : {
        hideInTable: true,
        search: false,
        width: '0%',
      };
};

export const useOperationStation = (props: {
  operationStatus: string;
  refreshList: () => void;
  width?: string | number;
}): ProColumns => {
  const { refreshList, width } = props;

  return {
    title: '操作',
    dataIndex: 'operation',
    align: 'center',
    search: false,
    width,
    render: (_, record) => {
      const { status, id } = record;
      return (
        <Space>
          <InvoiceTitle data={record} />
          {status == 0 && (
            <Popconfirm
              title='确定开票？'
              onConfirm={getGDinvoice.bind(null, {
                id,
                callback: refreshList,
              })}
            >
              <a>开票</a>
            </Popconfirm>
          )}
          {/* 拒绝原因 */}
          {status == 0 && <RejectReason id={id} status={-1} callback={refreshList} />}
        </Space>
      );
    },
  };
};
